package com.escom.utils;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.escom.R;
import com.escom.entidad.HistoriaItem;

import java.util.List;

/**
 * Created by Carlos on 16/04/2014.
 */
public class CustomHistoriaAdapter extends ArrayAdapter<HistoriaItem> {

    private Context context;
    private List<HistoriaItem> drawerItemHistoria;
    private int resource;

    public CustomHistoriaAdapter(Context context, int resource, List<HistoriaItem> drawerItemHistoria) {
        super(context, resource, drawerItemHistoria);
        this.context = context;
        this.drawerItemHistoria = drawerItemHistoria;
        this.resource = resource;
    }

    private static class PacienteViewHolder {
        TextView tVParentesco, tVEnfermedad;
        LinearLayout layoutHeader, layoutLabel;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        PacienteViewHolder pacienteViewHolder;
        View view = convertView;

        if (view == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            view = inflater.inflate(resource, parent, false);
            pacienteViewHolder = new PacienteViewHolder();
            pacienteViewHolder.tVParentesco = (TextView) view.findViewById(R.id.tVParentesco);
            pacienteViewHolder.tVEnfermedad = (TextView) view.findViewById(R.id.tVEnfermedad);
            pacienteViewHolder.layoutHeader = (LinearLayout) view.findViewById(R.id.layoutHeader);
            pacienteViewHolder.layoutLabel = (LinearLayout) view.findViewById(R.id.layoutLabel);
            view.setTag(pacienteViewHolder);
        } else {
            pacienteViewHolder = (PacienteViewHolder) view.getTag();
        }

        HistoriaItem historia = drawerItemHistoria.get(position);
        //Log.d(Constantes.LOG_ID, this.getClass().getName() + " Item: " + position + " " + historia.toString());

        if (historia.isTitle()) {
            pacienteViewHolder.tVParentesco.setText(historia.getLabelParentesco());
            pacienteViewHolder.layoutHeader.setVisibility(LinearLayout.VISIBLE);
            pacienteViewHolder.layoutLabel.setVisibility(LinearLayout.INVISIBLE);
        } else {
            pacienteViewHolder.tVEnfermedad.setText(historia.getLabelEnfermedad());
            pacienteViewHolder.layoutHeader.setVisibility(LinearLayout.INVISIBLE);
            pacienteViewHolder.layoutLabel.setVisibility(LinearLayout.VISIBLE);
        }

        return view;
    }
}
